﻿// Copyright (c) Inossem. All rights reserved.
// Licensed under the LGPL-3.0 license. See LICENSE file in the project root for full license information.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;

namespace DWF.Studio.RemoteCtrls
{
    /// <summary>
    /// 串行化参数集
    /// </summary>
    [Serializable]
    public class SerializablePairs
    {
        internal SerializablePairs()
        {
            this._parameters = new Dictionary<string, object>();
        }

        /// <summary>
        /// 参数字典
        /// </summary>
        private Dictionary<string, object> _parameters
        {
            get;
            set;
        }

        /// <summary>
        /// 总数
        /// </summary>
        public int Count
        {
            get
            {
                return _parameters.Count;
            }
        }

        public ICollection<string> Keys => throw new NotImplementedException();

        public ICollection<object> Values => throw new NotImplementedException();

        public bool IsReadOnly => throw new NotImplementedException();

        public object this[string key] { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }

        /// <summary>
        /// 是否可以串行化
        /// </summary>
        /// <param name="serializeableObject"></param>
        /// <returns></returns>
        public static bool IsSerializeable(object serializeableObject)
        {
            Type objType = serializeableObject.GetType();
            return (objType.IsPrimitive || objType.IsSerializable);
        }

        /// <summary>
        /// 获取参数
        /// </summary>
        /// <param name="parameterName"></param>
        /// <returns></returns>
        public object Get(string parameterName)
        {
            if (this._parameters.ContainsKey(parameterName))
            {
                return this._parameters[parameterName];
            }
            else
            {
                throw new KeyNotFoundException(parameterName);
            }
        }

        /// <summary>
        /// 设置参数
        /// </summary>
        /// <param name="parameterName"></param>
        /// <param name="serializeableObject"></param>
        public void Set(string parameterName, object serializeableObject)
        {
            Type paramType = serializeableObject.GetType();
            if (SerializablePairs.IsSerializeable(serializeableObject))
            {
                if (_parameters.ContainsKey(parameterName))
                {
                    _parameters[parameterName] = serializeableObject;
                }
                else
                {
                    throw new KeyNotFoundException(parameterName);
                }
            }
            else
            {
                throw new SerializationException(parameterName);
            }
        }

        /// <summary>
        /// 是否包含参数名
        /// </summary>
        /// <param name="parameterName"></param>
        /// <returns></returns>
        public bool Contains(string parameterName)
        {
            return this._parameters.ContainsKey(parameterName);
        }

        /// <summary>
        /// 获取枚举器
        /// </summary>
        /// <returns></returns>
        public Dictionary<string, object>.Enumerator GetEnumerator()
        {
            return this._parameters.GetEnumerator();
        }

        /// <summary>
        /// 转换成字典
        /// </summary>
        /// <returns></returns>
        public Dictionary<string, object> ToDictionary()
        {
            Dictionary<string, object> dic = new Dictionary<string, object>();

            foreach (string key in dic.Keys)
                dic.Add(key, dic[key]);

            return dic;
        }

        /// <summary>
        /// 添加参数
        /// </summary>
        /// <param name="parameterName"></param>
        /// <param name="serializeableObject"></param>
        public void Add(string parameterName, object serializeableObject)
        {
            Type paramType = serializeableObject.GetType();
            if (SerializablePairs.IsSerializeable(serializeableObject))
            {
                this._parameters.Add(parameterName, serializeableObject);
            }
            else
            {
                throw new SerializationException(parameterName);
            }
        }      
    }
}
